﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ITXHWEB.Controllers
{
    public partial class ABFOLGEController
    {

        [ChildActionOnly]
        public ActionResult SABP(string returnUrl)
        {
            ModelState.Clear();
            ViewBag.ReturnUrl = returnUrl;
            MABF.REMARK = INFO;
            return PartialView(MABF);
        }

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult SABP(ABFOLGEModel model, string returnUrl)
        {

            ModelState.Clear();
            MABF.ABPAUF = model.ABPAUF.Trim();
            ABFLOAD(model);
            if (MABF.IsABPReady)
            {
                INFO = "单号扫描正确";
            }
            else
            {
                INFO += model.ABPAUF + "不在有效范围内";
            }  
            return RedirectToAction(MAPP, MAPP);
        }
        public void ABFLOAD(ABFOLGEModel model)
        {

            MABF.IsABPReady = false;
            if (model.ABPAUF == null)
            {
                INFO = "model.ABPAUF == null";
                return;
            }

            if (model.ABPAUF.Trim().Length < 11)
            {
                INFO = "单号长度不足11";
                MABF.IsABPReady = false;
                return;
            }
            INFO = "扫描的生产单为" + model.ABPAUF;
            model.ABPAUF = model.ABPAUF.Trim().PadRight(11);
            PM["KRS"] = model.ABPAUF.Substring(0, 2);
            PM["LFD"] = model.ABPAUF.Substring(2, 6);
            PM["POS"] = model.ABPAUF.Substring(8, 3);
            INFO = "开始查询流程";
            SResult = VHP.VABP.LOADABF(PM);
            INFO = "开始装载流程:" + SResult.Tables.Count + ";";
            LOADABFS(SResult.Tables[0]);
            INFO = "装载流程信息结束";
            //此处可以自动定位到第一条未结束的
            var result = ABFS.Where(p => p.STATUS == " ").OrderByDescending(p => p.FSTEP).OrderBy(p => p.FSTEP);
            foreach (var m in result)
            {
                //STEPCHK(m);
                break;
            }

        }
    }
}
